<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>
<body>


<div id="loginFormDiv">
    <!-- 你的 HTML 代码 -->

    <form class="layui-form" action="" lay-filter="example">
        <h3 style="text-align: center; padding-bottom: 30px"> 请登录 </h3>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码框</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">验证码</label>
                <div class="layui-input-inline">
                    <input type="text" name="capchar" lay-verify="title" autocomplete="off" placeholder="请输入验证码"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <img src="/captcha" style="width:85px;height:38px" id="cap"/>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginFormBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

</div>

<script src="./layui/layui.js"></script>
<script>

    // layui框架的核心函数 layui.use(); 相当于 jquery的 $();
    layui.use(
        function () {
            // 代码起始位置

            var layer = layui.layer
                , form = layui.form
                , $ = layui.$;

            // layer.msg('Hello World');

            //监听提交
            form.on('submit(loginFormBtn)', function (data) {
                // layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // });
                console.log(data.field);
                // 发送ajxa请求
                $.ajax({
                    url: "/user/login",
                    type: "post",
                    data: data.field,
                    success: function (result) {
                        console.log(result);
                        if (result.statusCode == "1001") {
                            // 跳转到后台管理系统的主页面中
                            window.location.href = "/index.html";
                        }else {
                            layer.alert(result.message, {
                                title: '登录错误消息'
                            });
                        }
                    },
                    error: function (result) {
                        console.log(result);

                    }
                });

                return false; // 取消表单的默认提交行为
            });

            // 处理验证码点击后更换图片
            $("#cap").click(function () {
                //刷新验证码
                path = $(this).attr("src") + "?" + new Date().getTime();
                $(this).attr("src", path);
            });

            // 代码结束位置
        }
    );


</script>


<style>
    #loginFormDiv {
        margin: 100px auto;
        padding: 30px 30px 20px 50px;
        border: solid 1px blue;
        width: 600px;
        height: 300px;
        box-shadow: 5px 9px 18px 5px #eeeeee;
    }

</style>
</body>
</html>
